package com.example.demo1.inquiry.service.impl;


import com.example.demo1.inquiry.dao.InquiryMapper;
import com.example.demo1.inquiry.model.InquiryParts;
import com.example.demo1.inquiry.model.InquirySupplier;
import com.example.demo1.inquiry.model.InquiryTotal;
import com.example.demo1.inquiry.model.vo.*;
import com.example.demo1.inquiry.proxy.MatedataProxy;
import com.example.demo1.inquiry.service.InquiryService;

import com.example.demo1.wyy.model.LegalPerson;
import com.example.demo1.wyy.model.vo.LegalPersonVo;
import com.example.demo1.wyy.util.AllRecords;
import com.example.demo1.wyy.util.Result;
import com.example.demo1.wyy.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * Created by Lx on 2018/6/27.
 */
@Service
public class InquiryServiceImpl implements InquiryService {


    @Autowired
    private InquiryMapper inquiryMapper;
    @Autowired
    private MatedataProxy matedataProxy;

    /**
     * 询价单新增
     * @param inquiryAddVo
     * @return
     */
    @Override
    @Transactional
    public Result<InquiryAddVo> addInquiry(InquiryAddVo inquiryAddVo) {
        //验证数据是否合法
         Result<InquiryAddVo> inquiryAddVoResult = validate(inquiryAddVo);
        //操作数据入库。
        InquiryTotal  inquiryTotal = new InquiryTotal();
        BeanUtils.copyProperties(inquiryAddVo,inquiryTotal);

        Date nowDate = new Date();
        inquiryTotal.setId(751);
        inquiryTotal.setCreateId(2301);
        inquiryTotal.setStartDate(nowDate);
        inquiryTotal.setEndDate(nowDate);
        inquiryTotal.setCreateName("测试");
        inquiryTotal.setCreateTime(nowDate);
        inquiryTotal.setUpdateId(2301);
        inquiryTotal.setUpdateName("测试");
        inquiryTotal.setUpdateTime(nowDate);
        inquiryTotal.setInquiryNo(UUID.randomUUID().toString());
        inquiryMapper.saveInquiryTotal(inquiryTotal);
        // System.out.println(nowDate);
        //新增备件数据
        List<InquiryPartsVo> inquiryPartsVoList = inquiryAddVo.getPartsListVo();
        List<InquiryParts> inquiryPartsList = new ArrayList<>();
        //把VO集合的数据传输到实体集合中
        copyInquiryPartsVoToInquiryParts(inquiryPartsVoList,inquiryPartsList);
        for (InquiryParts inquiryParts :inquiryPartsList){
//            inquiryParts.setInquiryId(inquiryTotal.getId());
            //inquiryParts.setId(inquiryTotal.getId());
            inquiryParts.setCreateId(2301);
            inquiryParts.setCreateName("测试");
            inquiryParts.setCreateTime(nowDate);
            inquiryParts.setUpdateId(2301);
            inquiryParts.setUpdateName("测试");
            inquiryParts.setUpdateTime(nowDate);
            inquiryParts.setInquiryId(123);
            inquiryParts.setUnit("this");
            inquiryParts.setMoneyType(1);
            inquiryParts.setIntendDeliverDate(nowDate);

        }
        inquiryMapper.saveInquiryParts(inquiryPartsList);
        //新增供应商信息
        List<InquirySupplierVo> inquirySupplierVoList = inquiryAddVo.getSupplierListVo();
        List<InquirySupplier> inquirySuppliers = new ArrayList<>();
        //把VO集合的数据传输到实体集合中
        copySuppliersVoToSuppliers(inquirySupplierVoList,inquirySuppliers);
        for (InquirySupplier inquirySupplier :inquirySuppliers){
//            inquirySupplier.setInquiryId(inquiryTotal.getId());
            inquirySupplier.setId(6);
            inquirySupplier.setInquiryId(12);
            inquirySupplier.setCreateId(2301);
            inquirySupplier.setCreateName("测试");
            inquirySupplier.setCreateTime(nowDate);
            inquirySupplier.setUpdateId(2301);
            inquirySupplier.setUpdateName("测试");
            inquirySupplier.setUpdateTime(nowDate);
        }
        inquiryMapper.saveSupplier(inquirySuppliers);
        return ResultVO.success(inquiryAddVo);


    }

    public void copyInquiryPartsVoToInquiryParts(List<InquiryPartsVo> inquiryPartsVoList,List<InquiryParts> inquiryPartsList){
        for (InquiryPartsVo inquiryPartsVo:inquiryPartsVoList ){
            InquiryParts inquiryParts = new InquiryParts();
            BeanUtils.copyProperties(inquiryPartsVo,inquiryParts);
            inquiryPartsList.add(inquiryParts);
        }

    }
    public void copySuppliersVoToSuppliers( List<InquirySupplierVo> inquirySupplierVoList ,List<InquirySupplier> inquirySuppliers){
        for (InquirySupplierVo inquiryPartsVoList:inquirySupplierVoList ){
            InquirySupplier inquirySupplier = new InquirySupplier();
            BeanUtils.copyProperties(inquiryPartsVoList,inquirySupplier);
            inquirySuppliers.add(inquirySupplier);
        }

    }

    /**
     * 询价单修改
     * @param inquiryUpdateVo
     * @return
     */
    @Override
    @Transactional
    public Result<InquiryUpdateVo> updateInquiry(InquiryUpdateVo inquiryUpdateVo) {
     /*   //验证数据是否合法
        validateupdate(inquiryUpdateVo);
        //操作数据入库。
        //1.修改主表的数据
        //2.删除此询价单下备件表中的数据
        inquiryMapper.deletePartsById(inquiryUpdateVo.getId());
        //3.删除此询价单下供应商表中的数据
        //inquiryMapper.deletePartsBySupplier(inquiryUpdateVo.getSupplierListVo());
        //4.新增备件表中的数据
        //inquiryMapper.addPart(inquiryUpdateVo.getPartsListVo());
        //5.新增供应商表中的数据
        //inquiryMapper.addSupplier(inquiryUpdateVo.getSupplierListVo());
        return ResultVO.success(inquiryUpdateVo);*/

        //验证数据是否合法
        InquiryAddVo inquiryAddVo = new InquiryAddVo();
        BeanUtils.copyProperties(inquiryUpdateVo,inquiryAddVo);
        inquiryAddVo.setPartsListVo(inquiryUpdateVo.getPartsListVo());
        inquiryAddVo.setSupplierListVo(inquiryUpdateVo.getSupplierListVo());
        //验证数据是否合法
        Result<InquiryAddVo> inquiryAddVoResult = validate(inquiryAddVo);
        if (!inquiryAddVoResult.getStatus()){
            return ResultVO.error(inquiryAddVoResult.getCode(),inquiryAddVoResult.getMsg());
        }
        //操作数据入库。
        Date nowDate = new Date();
        InquiryTotal inquiryTotal = new InquiryTotal();
        BeanUtils.copyProperties(inquiryUpdateVo,inquiryTotal);
        inquiryTotal.setUpdateId(2301);
        inquiryTotal.setStartDate(nowDate);
        inquiryTotal.setEndDate(nowDate);
        inquiryTotal.setUpdateName("测试");
        inquiryTotal.setUpdateTime(nowDate);
        inquiryTotal.setMoneyType(2);
//        //1.修改主表的数据
//        inquiryMapper.updateInquiry(inquiryTotal);
        //1.修改主表的数据
        int count = inquiryMapper.updateInquiry(inquiryTotal);
        if (count ==0){
            return ResultVO.error(500,"修改对象不存在，刷新后重试！");
        }
        //2.删除此询价单下备件表中的数据
        inquiryMapper.deletePartsById(inquiryUpdateVo.getId());
        //3.删除此询价单下供应商表中的数据
        inquiryMapper.deleteSupplierById(inquiryUpdateVo.getId());
        //4.新增备件表中的数据
        List<InquiryPartsVo> inquiryPartsVoList = inquiryUpdateVo.getPartsListVo();
        List<InquiryParts> inquiryPartsList = new ArrayList<>();
        //把VO集合的数据传输到实体集合中
        copyInquiryPartsVoToInquiryParts(inquiryPartsVoList,inquiryPartsList);
        for (InquiryParts inquiryParts :inquiryPartsList){
            inquiryParts.setInquiryId(inquiryTotal.getId());
            inquiryParts.setMoneyType(4);
            inquiryParts.setIntendDeliverDate(nowDate);
            inquiryParts.setCreateName("SpringBoot");
            inquiryParts.setCreateTime(nowDate);
            inquiryParts.setCreateId(1);
            inquiryParts.setUpdateName("SpringBootUpdate");
            inquiryParts.setUpdateTime(nowDate);
            inquiryParts.setUpdateId(1);
        }
        inquiryMapper.saveInquiryParts(inquiryPartsList);
        //5.新增供应商表中的数据
        List<InquirySupplierVo> inquirySupplierVoList = inquiryUpdateVo.getSupplierListVo();
        List<InquirySupplier> inquirySuppliers = new ArrayList<>();
        //把VO集合的数据传输到实体集合中
        copySuppliersVoToSuppliers(inquirySupplierVoList,inquirySuppliers);
        for (InquirySupplier inquirySupplier :inquirySuppliers){
            inquirySupplier.setInquiryId(inquiryTotal.getId());
            inquirySupplier.setSupplierCode("SpringCloud");
        }
        inquiryMapper.saveSupplier(inquirySuppliers);
        return ResultVO.success(inquiryUpdateVo);
    }

    public Result<InquiryAddVo> validateupdate(InquiryUpdateVo  inquiryUpdateVo){
          //验证数据是否合法
        Result<InquiryUpdateVo> plantAddVoResult ;//= validDataUpdate(inquiryUpdateVo);
      /*  if (!plantAddVoResult.getStatus()){
            return ResultVO.error(plantAddVoResult.getCode(),plantAddVoResult.getMsg());
        }*/
        InquiryTotal  inquiryTotal = new InquiryTotal();
        BeanUtils.copyProperties(inquiryUpdateVo,inquiryTotal);
        //InquiryMapper.saveInquiryTotal(inquiryTotal);
        return ResultVO.success(inquiryTotal);

       /* List<String> partsCodeList = new ArrayList<>();
        for (InquiryPartsVo inquiryParts :inquiryAddVo.getPartsListVo()){
            partsCodeList.add(inquiryParts.getCode());
        }
        List<String> supplierCodeList = new ArrayList<>();
        for (InquirySupplierVo inquirySupplierVo :inquiryAddVo.getSupplierListVo()){
            supplierCodeList.add(inquirySupplierVo.getSupplierCode());
        }
        //1.验证备件编码、供应商是否重复
        Result<InquiryAddVo> inquiryAddVoResult =  validatePartsSupplierIsRepeat(inquiryAddVo,partsCodeList,supplierCodeList);
        if (!inquiryAddVoResult.getStatus()){
            return ResultVO.error(inquiryAddVoResult.getCode(),inquiryAddVoResult.getMsg());
        }
        //2.验证备件编码、供应商是否存在
        validatePartsSupplierIsExist(inquiryAddVo,partsCodeList,supplierCodeList);
        //3.验证供应商是否是备件的合法供应商
        validatePartsSupplierIsLegal(inquiryAddVo,partsCodeList,supplierCodeList);
        //4.同一法人同一开始截止时间是否是唯一数据
//        validateData(inquiryAddVo);
        return ResultVO.success(inquiryAddVo);*/
    }
/*    private Result<InquiryUpdateVo> validDataUpdate(InquiryUpdateVo  inquiryUpdateVo){
        //验证工厂编码是否唯一，传入工厂编码到数据库中查询是否存在工厂编码
        List<InquiryTotal> inquiryList = InquiryMapper.findByCode(inquiryUpdateVo.getId());
        if (!CollectionUtils.isEmpty(inquiryList)){
            return ResultVO.error(500,"询价单编码已存在！");
        }
        //验证法人编码是否存在
        InquiryTotal  inquiryTotal = InquiryMapper.findOne(inquiryUpdateVo.getInquiryNo());
        if (inquiryTotal==null){
            return ResultVO.error(500,"询价单信息不存在！");
        }
        return ResultVO.success(inquiryUpdateVo);
    }*/

    public Result<InquiryAddVo> validate(InquiryAddVo inquiryAddVo){
        //验证法人存在
        LegalPersonVo legalPerson = new LegalPersonVo();
        legalPerson.setId(inquiryAddVo.getLegalPersonId());
        Result<LegalPersonVo> legalPersonResult=matedataProxy.queryByLegalPerson( legalPerson );
        System.out.println("-------------AAAAA----   "+legalPersonResult);
        List<String> partsCodeList = new ArrayList<>();
        for (InquiryPartsVo inquiryParts :inquiryAddVo.getPartsListVo()){
            partsCodeList.add(inquiryParts.getPartsCode());
        }
        List<String> supplierCodeList = new ArrayList<>();
        for (InquirySupplierVo inquirySupplierVo :inquiryAddVo.getSupplierListVo()){
//            System.out.println("&&&&  &&&&--------------------------&      "+inquirySupplierVo.getSupplierCode());
            supplierCodeList.add(inquirySupplierVo.getSupplierCode());
        }
        //1.验证备件编码、供应商是否重复
        Result<InquiryAddVo> inquiryAddVoResult =  validatePartsSupplierIsRepeat(inquiryAddVo,partsCodeList,supplierCodeList);
        if (!inquiryAddVoResult.getStatus()){
            return ResultVO.error(inquiryAddVoResult.getCode(),inquiryAddVoResult.getMsg());
        }
        //2.验证备件编码、供应商是否存在
        validatePartsSupplierIsExist(inquiryAddVo,partsCodeList,supplierCodeList);
        //3.验证供应商是否是备件的合法供应商
        validatePartsSupplierIsLegal(inquiryAddVo,partsCodeList,supplierCodeList);

        //4.同一法人同一开始截止时间是否是唯一数据
//        validateData(inquiryAddVo);
        return ResultVO.success(inquiryAddVo);
    }

    public Result<InquiryAddVo> validatePartsSupplierIsLegal(InquiryAddVo inquiryAddVo,List<String> partsCodeList,List<String> supplierCodeList){
        List<InquiryParts> inquiryParts = inquiryMapper.queryPartsBySupplierCodes(supplierCodeList);
        List<String> partsCode = new ArrayList<>();
        for (InquiryParts inquiryParts1:inquiryParts){
            partsCode.add(inquiryParts1.getPartsCode());
        }
        for(String code:partsCodeList){
            if(!partsCode.contains(code)){
                return ResultVO.error(500,"供应商不合法："+code);
            }
        }
        return ResultVO.success(inquiryAddVo);
    }


    public Result<InquiryAddVo> validatePartsSupplierIsExist(InquiryAddVo inquiryAddVo,List<String> partsCodeList,List<String> supplierCodeList){

        List<InquiryParts> inquiryParts = inquiryMapper.queryPartsByCodes(partsCodeList);
        List<String> inquiryCodeList = new ArrayList<>();
        for (InquiryParts inquiryParts1 :inquiryParts){
            inquiryCodeList.add(inquiryParts1.getPartsCode());
        }
        if (inquiryCodeList.size()!=partsCodeList.size()){
            partsCodeList.removeAll(inquiryCodeList);
            return ResultVO.error(500,"备件编码不存在,不存在编码为："+inquiryParts);
        }
        List<InquirySupplier> supplierList = inquiryMapper.querySupplierByCodes(supplierCodeList);
        List<String> supplierCodes = new ArrayList<>();
        for (InquirySupplier supplier :supplierList){
            supplierCodes.add(supplier.getSupplierCode());
        }
        if (supplierCodeList.size()!=supplierCodes.size()){
            supplierCodeList.removeAll(supplierCodes);
            return ResultVO.error(500,"供应商编码不存在,不存在编码为："+inquiryParts);
        }
        return ResultVO.success(inquiryAddVo);
    }

    public Result<InquiryAddVo> validatePartsSupplierIsRepeat(InquiryAddVo inquiryAddVo,List<String> partsCodeList,List<String> supplierCodeList){

       /* List<InquiryPartsVo> inquiryPartsVoList =  inquiryAddVo.getPartsListVo();

        //排重
        partsCodeList = new ArrayList<>(new HashSet<>(partsCodeList));
        if (partsCodeList.size()!=inquiryPartsVoList.size()){
            inquiryPartsVoList.removeAll(partsCodeList);
            return ResultVO.error(500,"备件编码重复,重复编码为："+inquiryPartsVoList);
        }

        List<InquirySupplierVo> inquirySupplierVos =  inquiryAddVo.getSupplierListVo();

        //排重
        supplierCodeList = new ArrayList<>(new HashSet<>(supplierCodeList));
        if (supplierCodeList.size()!=inquirySupplierVos.size()){
            inquirySupplierVos.removeAll(supplierCodeList);
            return ResultVO.error(500,"供应商编码重复,重复编码为："+inquiryPartsVoList);
        }
        return ResultVO.success(inquiryAddVo);*/

        //验证备件编码是否重复，如果存在重复的数据，提示出所有重复数据
        List<String> codeList = new ArrayList<>();
        List<String> codeErrorList = new ArrayList<>();
        for (String code:partsCodeList){
            if (!codeList.contains(code)){
                codeList.add(code);
            }else {
                codeErrorList.add(code);
            }
        }
        if (codeErrorList.size()>0){
            return ResultVO.error(500,"备件编码重复,重复编码为："+codeErrorList);
        }
        //验证供应商是否重复，如果存在重复的数据，提示出所有重复数据
        List<String> supplierList = new ArrayList<>();
        List<String> supplierCodeErrorList = new ArrayList<>();
        for (String code:supplierCodeList){
            if (!supplierList.contains(code)){
                supplierList.add(code);
            }else {
                supplierCodeErrorList.add(code);
            }
        }
        if (supplierCodeErrorList.size()>0){
            return ResultVO.error(500,"供应商编码重复,重复编码为："+supplierCodeErrorList);
        }
        return ResultVO.success(inquiryAddVo);
    }
    public Result<AllRecords<InquiryTotal>> queryPage(InquiryQueryVo inquiryQueryVo){
        AllRecords allRecords = new AllRecords();
        List<InquiryTotal> inquiryTotals = inquiryMapper.queryPage(inquiryQueryVo);
        int allCount = inquiryMapper.queryCount(inquiryQueryVo);
        allRecords.setPageSize(inquiryQueryVo.getPageSize());
        allRecords.setPageIndex(inquiryQueryVo.getPageIndex());
        allRecords.setTotalNumber(allCount);
        allRecords.resetTotalNumber(allCount);
        allRecords.setDataList(inquiryTotals);
        return ResultVO.success(allRecords);
    }

    public Result<InquiryTotalVo> seeInquiry(Integer id){
        //查看询价单表头信息
        InquiryTotalVo inquiryTotalVo = inquiryMapper.seeInquiry(id);
        //查看备件信息
        List<InquiryPartsVo> inquiryPartsVoList = inquiryMapper.queryPartsByTotalId(id);
        //查看供应商信息
        List<InquirySupplierVo> inquirySupplierVos = inquiryMapper.querySupplierByTotalId(id);
        inquiryTotalVo.setPartsList(inquiryPartsVoList);
        inquiryTotalVo.setSupplierList(inquirySupplierVos);
        return ResultVO.success(inquiryTotalVo);
    }
}


